{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7.9 \u5c06\u5355\u65b9\u6cd5\u7684\u7c7b\u8f6c\u6362\u4e3a\u51fd\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u9664 __init__() \u65b9\u6cd5\u5916\u53ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5\u7684\u7c7b\u3002\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\uff0c\u4f60\u60f3\u5c06\u5b83\u8f6c\u6362\u6210\u4e00\u4e2a\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u6765\u5c06\u5355\u4e2a\u65b9\u6cd5\u7684\u7c7b\u8f6c\u6362\u6210\u51fd\u6570\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u793a\u4f8b\u4e2d\u7684\u7c7b\u5141\u8bb8\u4f7f\u7528\u8005\u6839\u636e\u67d0\u4e2a\u6a21\u677f\u65b9\u6848\u6765\u83b7\u53d6\u5230URL\u94fe\u63a5\u5730\u5740\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib.request import urlopen\n\nclass UrlTemplate:\n    def __init__(self, template):\n        self.template = template\n\n    def open(self, **kwargs):\n        return urlopen(self.template.format_map(kwargs))\n\n# Example use. Download stock data from yahoo\nyahoo = UrlTemplate('http://finance.yahoo.com/d/quotes.csv?s={names}&f={fields}')\nfor line in yahoo.open(names='IBM,AAPL,FB', fields='sl1c1v'):\n    print(line.decode('utf-8'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u88ab\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u51fd\u6570\u6765\u4ee3\u66ff\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def urltemplate(template):\n    def opener(**kwargs):\n        return urlopen(template.format_map(kwargs))\n    return opener\n\n# Example use\nyahoo = urltemplate('http://finance.yahoo.com/d/quotes.csv?s={names}&f={fields}')\nfor line in yahoo(names='IBM,AAPL,FB', fields='sl1c1v'):\n    print(line.decode('utf-8'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u4f60\u62e5\u6709\u4e00\u4e2a\u5355\u65b9\u6cd5\u7c7b\u7684\u539f\u56e0\u662f\u9700\u8981\u5b58\u50a8\u67d0\u4e9b\u989d\u5916\u7684\u72b6\u6001\u6765\u7ed9\u65b9\u6cd5\u4f7f\u7528\u3002\n\u6bd4\u5982\uff0c\u5b9a\u4e49UrlTemplate\u7c7b\u7684\u552f\u4e00\u76ee\u7684\u5c31\u662f\u5148\u5728\u67d0\u4e2a\u5730\u65b9\u5b58\u50a8\u6a21\u677f\u503c\uff0c\u4ee5\u4fbf\u5c06\u6765\u53ef\u4ee5\u5728open()\u65b9\u6cd5\u4e2d\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u6216\u8005\u95ed\u5305\u7684\u65b9\u6848\u901a\u5e38\u4f1a\u66f4\u4f18\u96c5\u4e00\u4e9b\u3002\u7b80\u5355\u6765\u8bb2\uff0c\u4e00\u4e2a\u95ed\u5305\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\n\u53ea\u4e0d\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u5e26\u4e0a\u4e86\u4e00\u4e2a\u989d\u5916\u7684\u53d8\u91cf\u73af\u5883\u3002\u95ed\u5305\u5173\u952e\u7279\u70b9\u5c31\u662f\u5b83\u4f1a\u8bb0\u4f4f\u81ea\u5df1\u88ab\u5b9a\u4e49\u65f6\u7684\u73af\u5883\u3002\n\u56e0\u6b64\uff0c\u5728\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u4e2d\uff0copener() \u51fd\u6570\u8bb0\u4f4f\u4e86 template \u53c2\u6570\u7684\u503c\uff0c\u5e76\u5728\u63a5\u4e0b\u6765\u7684\u8c03\u7528\u4e2d\u4f7f\u7528\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u4f60\u78b0\u5230\u9700\u8981\u7ed9\u67d0\u4e2a\u51fd\u6570\u589e\u52a0\u989d\u5916\u7684\u72b6\u6001\u4fe1\u606f\u7684\u95ee\u9898\uff0c\u90fd\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u95ed\u5305\u3002\n\u76f8\u6bd4\u5c06\u4f60\u7684\u51fd\u6570\u8f6c\u6362\u6210\u4e00\u4e2a\u7c7b\u800c\u8a00\uff0c\u95ed\u5305\u901a\u5e38\u662f\u4e00\u79cd\u66f4\u52a0\u7b80\u6d01\u548c\u4f18\u96c5\u7684\u65b9\u6848\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}